package com.example.demo;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-06-12
 * Time: 16:25
 */
// 邮箱发送工具类
public class EmailTools {

    /**
     * @param to    收件人邮箱
     * @param text  邮件正文
     * @param title 邮件标题
     */

    public static boolean sendMail(String to, String text, String title) {

        final String USER = "1843977391@qq.com"; // 发件人邮箱
        final String PASSWORD = "osemsbwrhynkecjb"; // 发件人授权码

        try {
            Properties properties = new Properties();
            properties.put("mail.smtp.auth", "true");
            // 注意发送邮件中的方法, 发送给谁的, 发送给对应的app
            // 要改成对应的 app  smtp.163.com

            properties.put("mail.smtp.host", "smtp.qq.com"); // QQ 邮箱的

            // 发件人的账号
            properties.put("mail.user", USER);
            // 发件人的密码
            properties.put("mail.password", PASSWORD);

            // 构建授权信息, 用于进行 SMTP 身份验证
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    String userName = properties.getProperty("mail.user");
                    String password = properties.getProperty("mail.password");
                    return new PasswordAuthentication(userName, password);
                }
            };

            // 使用环境属性和授权信息, 创建邮件会话
            Session mailSession = Session.getInstance(properties, authenticator);
            // 创建邮件消息
            MimeMessage message = new MimeMessage(mailSession);
            // 设置发件人
            String username = properties.getProperty("mail.user");
            InternetAddress from = new InternetAddress(username);
            message.setFrom(from);

            // 设置收件人
            InternetAddress toAddress = new InternetAddress(to);
            message.setRecipient(Message.RecipientType.TO, toAddress);

            // 设置邮件的标题
            message.setSubject(title);
            // 设置邮件内容
            message.setContent(text, "text/html; charset = utf8");
            // 发送邮件
            Transport.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    public static boolean sendMail1(String to, String text, String title) {

        final String USER = "zjingyin0524@163.com"; // 发件人邮箱
        final String PASSWORD = "OFAAFROKJMNBNXFI"; // 发件人授权码

        try {
            Properties properties = new Properties();
            properties.put("mail.smtp.auth", "true");
            // 注意发送邮件中的方法, 发送给谁的, 发送给对应的app
            // 要改成对应的 app  smtp.163.com

            properties.put("mail.smtp.host", "smtp.163.com"); // QQ 邮箱的

            // 发件人的账号
            properties.put("mail.user", USER);
            // 发件人的密码
            properties.put("mail.password", PASSWORD);

            // 构建授权信息, 用于进行 SMTP 身份验证
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    String userName = properties.getProperty("mail.user");
                    String password = properties.getProperty("mail.password");
                    return new PasswordAuthentication(userName, password);
                }
            };

            // 使用环境属性和授权信息, 创建邮件会话
            Session mailSession = Session.getInstance(properties, authenticator);
            // 创建邮件消息
            MimeMessage message = new MimeMessage(mailSession);
            // 设置发件人
            String username = properties.getProperty("mail.user");
            InternetAddress from = new InternetAddress(username);
            message.setFrom(from);

            // 设置收件人
            InternetAddress toAddress = new InternetAddress(to);
            message.setRecipient(Message.RecipientType.TO, toAddress);

            // 设置邮件的标题
            message.setSubject(title);
            // 设置邮件内容
            message.setContent(text, "text/html; charset = utf8");
            // 发送邮件
            Transport.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    public static void main(String[] args) {
        Random random = new Random();
        int code = random.nextInt(10_00000 - 100000) + 100000; // 生成一个 六位数的随机验证码
        System.out.println(code);
        EmailTools.sendMail1("1843977391@qq.com", "你得验证码为: " +  code + ", 请妥善保管,请勿轻易告知他人!", "欢迎注册博客!");
        System.out.println("发送成功");
    }
}
